<?php

namespace iBrand\Component\Purchase\Models;

use iBrand\Shop\Core\Models\Clerk;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

class Purchase extends Model implements Transformable
{
    use SoftDeletes, TransformableTrait;

    const TYPE_PURCHASE = 'PURCHASE';  //采购
    const TYPE_REJECT = 'REJECT';   //采购退货

    const STATUS_TMP = 2;
    const STATUS_DEFAULT = 1;

    protected $guarded = ['id'];

    protected $appends = ['type_text'];

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        $prefix = config('ibrand.app.database.prefix', 'ibrand_');

        $this->setTable($prefix . 'store_purchase');
    }

    public function items()
    {
        return $this->hasMany(PurchaseItem::class, 'purchase_id');
    }

    public function clerk()
    {
        return $this->belongsTo(Clerk::class, 'clerk_id')->withDefault();
    }

    public function getTypeTextAttribute()
    {
        $text = '';
        if ($this->type == self::TYPE_PURCHASE) {
            $text = '采购进货';
        }

        if ($this->type == self::TYPE_REJECT) {
            $text = '采购退货';
        }

        return $text;
    }
}